package com.cooperation;


import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CyclicBarrierExample {

//
//    public CyclicBarrier(int parties, Runnable barrierAction) {
//        if (parties <= 0) throw new IllegalArgumentException();
//        this.parties = parties;
//        this.count = parties;
//        this.barrierCommand = barrierAction;
//    }
//
//    public CyclicBarrier(int parties) {
//        this(parties, null);
//    }


    /**
     * 用来控制多个线程互相等待，只有当多个线程对到达时，这些线程才会继续执行。
     * @param args
     */
    public static void main(String[] args) {
        final int totalThread=10;
        CyclicBarrier cyclicBarrier=new CyclicBarrier(totalThread);
        ExecutorService executorService= Executors.newCachedThreadPool();
        for (int i=0;i<totalThread;i++) {
            executorService.execute(() -> {
                System.out.print("before...");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.print("after..");

            });
        }
        executorService.shutdown();

    }


}
